package client.ui.component.card;

import base.BaseValue;
import client.assets.Icons;
import client.connect.HttpConnect;
import client.util.ImageUtil;

import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class PictureLabel extends JLabel {
    protected boolean hover = false;
    public PictureLabel(String url, Icon placeHoldImage, int horizontalAlignment) {
        super(placeHoldImage, horizontalAlignment);
        new Thread(new Runnable() {
            @Override
            public void run() {
                ImageIcon imageIcon = Icons.place_hold;
                try {
                    imageIcon = new ImageIcon(new URL(url));
                } catch (MalformedURLException ignored) {}
                if (imageIcon.getIconWidth() > imageIcon.getIconHeight()){
                    imageIcon = ImageUtil.getImageIcon(imageIcon,null,180,0);
                } else {
                    imageIcon = ImageUtil.getImageIcon(imageIcon,null,0,180);
                }
                ImageIcon finalImageIcon = imageIcon;
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        PictureLabel.this.setIcon(finalImageIcon);
                    }
                });
            }
        }).start();
    }

    public PictureLabel(Icon image, int horizontalAlignment) {
        super(image, horizontalAlignment);
    }

    public PictureLabel(Icon image) {
        super(image);
    }

    public PictureLabel() {
        super();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(hover) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(new Color(255,255,255,60));
            g2.fillRect(0,0,getWidth(),getHeight());
            g2.dispose();
        }
    }

}
